package com.polaris.live.dialog

import android.content.Context
import androidx.recyclerview.widget.GridLayoutManager
import com.blankj.utilcode.util.StringUtils
import com.lxj.xpopup.XPopup
import com.lxj.xpopup.core.CenterPopupView
import com.polaris.live.R
import com.polaris.live.adapter.LoginRewardAdapter
import com.polaris.live.databinding.DialogRewardActiveBinding
import com.polaris.live.resp.back_resp.ActiveTask
import com.polaris.live.translation.R.string

/**
 * 活跃礼物弹窗说明
 *
 * @author Created by lucas on 2023/10/28 09:28
 */
class ActiveRewardDialog(
    context: Context,
    private val isWeek: Boolean,
    private val activeTask: ActiveTask,
) : CenterPopupView(context) {

    private lateinit var mBinding: DialogRewardActiveBinding

    override fun getImplLayoutId(): Int {
        return R.layout.dialog_reward_active
    }

    private val mAdapter: LoginRewardAdapter by lazy {
        LoginRewardAdapter()
    }

    override fun onCreate() {
        super.onCreate()
        mBinding = DialogRewardActiveBinding.bind(popupImplView)

        mBinding.tvTitle.text = StringUtils.getString(string.active_rewards)

        if (isWeek) {
            mBinding.tvContent.text = StringUtils.getString(string.active_rewards_week, activeTask.value)
        } else {
            mBinding.tvContent.text = StringUtils.getString(string.active_rewards_day, activeTask.value)
        }

        val size = if (activeTask.rewards.size > 3) 3 else activeTask.rewards.size
        mBinding.recyclerLogin.apply {
            layoutManager = GridLayoutManager(context, size)
            adapter = mAdapter
            itemAnimator = null
        }
        mAdapter.setList(activeTask.rewards)
    }

    companion object {

        fun show(
            context: Context,
            isWeek: Boolean,
            activeTask: ActiveTask,
        ) {
            XPopup.Builder(context)
                .dismissOnTouchOutside(true)
                .dismissOnBackPressed(true)
                .isDestroyOnDismiss(true)
                .asCustom(
                    ActiveRewardDialog(
                        context,
                        isWeek,
                        activeTask
                    )
                )
                .show()
        }

    }
}